package org.linlinjava.litemall.db.service;

import com.github.pagehelper.PageHelper;
import org.linlinjava.litemall.db.dao.LitemallUserLevelMapper;
import org.linlinjava.litemall.db.dao.LitemallUserMapper;
import org.linlinjava.litemall.db.domain.LitemallUser;
import org.linlinjava.litemall.db.domain.LitemallUserLevel;
import org.linlinjava.litemall.db.domain.LitemallUserLevelExample;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@Service
public class LitemallUserLevelService {
    @Resource
    private LitemallUserLevelMapper userLevelMapper;
    @Resource
    private LitemallUserMapper userMapper;

    public void saveUserLevel(LitemallUserLevel withdraw) {
        userLevelMapper.insert(withdraw);
    }

    public List<LitemallUserLevel> queryAdminList(Byte status, Integer page, Integer limit, String sort, String order) {
        LitemallUserLevelExample example = new LitemallUserLevelExample();
        LitemallUserLevelExample.Criteria criteria = example.or();
        if(status!=null){
            criteria.andStatusEqualTo(status);
        }
        criteria.andDeletedEqualTo(false);
        example.setOrderByClause(sort + " " + order);
        PageHelper.startPage(page, limit);
        return userLevelMapper.selectByExample(example);
    }

    public LitemallUserLevel findById(Integer id) {
        return userLevelMapper.selectByPrimaryKey(id);
    }

    public BigDecimal vipPrice(BigDecimal orderTotalPrice, Integer userId) {

        LitemallUser user = userMapper.selectByPrimaryKey(userId);
        LitemallUserLevelExample example = new LitemallUserLevelExample();
        LitemallUserLevelExample.Criteria criteria = example.or();
        criteria.andLevelEqualTo(user.getUserLevel()).andLogicalDeleted(false).andStatusEqualTo((byte) 0);
        LitemallUserLevel userLevel = userLevelMapper.selectOneByExample(example);
        BigDecimal discount = userLevel.getDiscount();

        return orderTotalPrice.multiply(discount);
    }
}
